package com.syezon.wifikey.pwd.viewmodel;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.arch.lifecycle.MutableLiveData;
import android.support.annotation.NonNull;

import com.syezon.wifikey.app.WiFiKeyApp;
import com.syezon.wifikey.wifi.info.WifiInfo;
import com.syezon.wifikey.pwd.info.SaveWifiPwdInfo;
import com.syezon.wifikey.utils.Utils;

import java.util.Iterator;
import java.util.List;

/**
 * Created by Administrator on 2018/9/10.
 */

public class MyPwdViewModel extends AndroidViewModel {

    private MutableLiveData<List<SaveWifiPwdInfo>> saveWifiListLiveData = new MutableLiveData<>();

    private static MyPwdViewModel myPwdViewModel;

    private MyPwdViewModel(@NonNull Application application) {
        super(application);
    }

    public static MyPwdViewModel getInstance() {
        if (myPwdViewModel == null) {
            synchronized (MyPwdViewModel.class) {
                if (myPwdViewModel == null) {
                    myPwdViewModel = new MyPwdViewModel(Utils.getApp());
                }
            }
        }
        return myPwdViewModel;
    }

    public MutableLiveData<List<SaveWifiPwdInfo>> getSavePwdInfoListLiveData() {
        return saveWifiListLiveData;
    }

    public boolean isSavePwd(String ssid) {
        boolean isSave = false;
        try {
            List<SaveWifiPwdInfo> all = WiFiKeyApp.getDatabase().getWifiPwdDao().getAll();
            for (int i = 0; all != null && i < all.size(); i++) {
                SaveWifiPwdInfo saveWifiPwdInfo = all.get(i);
                if (saveWifiPwdInfo.getSsid().equals(ssid)) {
                    isSave = true;
                    break;
                }
            }
        } catch (Exception e) {
        }
        return isSave;
    }

    /**
     * 获取保存在本地wifi密码列表
     */
    public void getSaveWifiList() {
        try {
            List<SaveWifiPwdInfo> all = WiFiKeyApp.getDatabase().getWifiPwdDao().getAll();
//            List<WifiInfo> wifiInfoList = WifiListService.getInstance().getWifiBeanList();
//            for (int i = 0; all != null && i < all.size(); i++) {
//                SaveWifiPwdInfo saveWifiPwdInfo = all.get(i);
//                Iterator<WifiInfo> iterator = wifiInfoList.iterator();
//                while (iterator.hasNext()) {
//                    WifiInfo wifiInfo = iterator.next();
//                    if (saveWifiPwdInfo.getSsid().equals(wifiInfo.getSsid())) {
//                        saveWifiPwdInfo.setWifiInfo(wifiInfo);
//                        break;
//                    }
//                }
//            }
            saveWifiListLiveData.postValue(all);
        } catch (Exception e) {

        }
    }
}
